class MapManager {
	constructor(map) {
		this._map = map;
	}

	getEmptySource(data) {
		return {
			"type": "geojson",
			'data': {
				"type": "FeatureCollection",
				"features": !!data && data.length > 0 ? data : []
			}
		}
	}

	//添加SourceAndLayer
	addSourceAndLayer() {
		return new Promise((resolve, reject) => {
			let map = this._map;
			//市9  区10  街道11  社区13  网格15  16
			//市SBM  区QBM  街道JDBM  社区SQBM  网格JCWGBM

			//-------------市 SBM   city-------------------
			if (!map.getSource("city")) {
				map.addSource("city", this.getEmptySource());
			}

			if (!map.getLayer("city-fills")) {
				map.addLayer({
					"id": "city-fills",
					"type": "fill",
					"source": "city",
					"minzoom": 8,
					"layout": {},
					"paint": {
						"fill-color": "#296CF2",
						"fill-opacity": 0
					}
				});
			}

			if (!map.getLayer("city-borders")) {
				map.addLayer({
					"id": "city-borders",
					"type": "line",
					"source": "city",
					"minzoom": 8,
					"layout": {},
					"paint": {
						"line-color": "#296CF2", //红色
						"line-width": 2
					}
				});
			}


			//-------------区 QBM   region------------------
			if (!map.getSource("region")) {
				map.addSource("region", this.getEmptySource());
			}
			if (!map.getLayer("region-fills")) {
				map.addLayer({
					"id": "region-fills",
					"type": "fill",
					"source": "region",
					"minzoom": 9,
					"layout": {},
					"paint": {
						"fill-color": "#296CF2",
						"fill-opacity": 0
					}
				});
			}

			if (!map.getLayer("region-borders")) {
				map.addLayer({
					"id": "region-borders",
					"type": "line",
					"source": "region",
					"minzoom": 9,
					"layout": {},
					"paint": {
						"line-color": "#296CF2",
						"line-width": 2
					}
				}, 'city-borders'); //设置在区级线的下面
			}

			//------------街道 JDBM   street----------------
			if (!map.getSource("street")) {
				map.addSource("street", this.getEmptySource());
			}
			if (!map.getLayer("street-fills")) {
				map.addLayer({
					"id": "street-fills",
					"type": "fill",
					"source": "street",
					"minzoom": 10,
					"layout": {},
					"paint": {
						"fill-color": "#0BB9C2",
						"fill-opacity": 0
					}
				});
			}

			if (!map.getLayer("street-borders")) {
				map.addLayer({
					"id": "street-borders",
					"type": "line",
					"source": "street",
					"minzoom": 10,
					"layout": {},
					"paint": {
						"line-color": "#0BB9C2", //红色
						"line-width": 2
					}
				}, 'region-borders');
			}


			//-------------社区  community -----------------
			if (!map.getSource("community")) {
				map.addSource("community", this.getEmptySource());
			}
			if (!map.getLayer("community-fills")) {
				map.addLayer({
					"id": "community-fills",
					"type": "fill",
					"source": "community",
					"minzoom": 12,
					"layout": {},
					"paint": {
						"fill-color": "#22A25B",
						"fill-opacity": 0
					}
				});
			}

			if (!map.getLayer("community-borders")) {
				map.addLayer({
					"id": "community-borders",
					"type": "line",
					"source": "community",
					"minzoom": 12,
					"layout": {},
					"paint": {
						// "line-color": "#59A5F5",  //蓝色
						"line-color": "#22A25B", //红色
						"line-width": 2
					}
				}, 'street-borders');
			}

			if (!map.getLayer("community-hover")) {
				//社区高亮颜色图层
				map.addLayer({
					"id": "community-hover",
					"type": "line",
					"source": "community",
					"minzoom": 12,
					"paint": {
						"line-color": "#22A25B",
						"line-width": 2
					},
					"filter": ["==", "SQBM", '']
				});
			}


			//---------------网格  grid-------------------
			if (!map.getSource("grid")) {
				map.addSource("grid", this.getEmptySource());
			}
			if (!map.getLayer("grid-fills")) {
				map.addLayer({
					"id": "grid-fills",
					"type": "fill",
					"source": "grid",
					"minzoom": 15,
					"layout": {},
					"paint": {
						"fill-color": "#ff9e67",
						"fill-opacity": 0
					}
				});
			}

			if (!map.getLayer("grid-borders")) {
				map.addLayer({
					"id": "grid-borders",
					"type": "line",
					"source": "grid",
					"minzoom": 15,
					"layout": {},
					"paint": {
						"line-color": "#ff9e67", //#ff9e67
						"line-width": 1
					}
				}, 'community-borders');
			}

			if (!map.getLayer("grid-fills-hover")) {
				map.addLayer({
					"id": "grid-fills-hover",
					"type": "fill",
					"source": "grid",
					"minzoom": 15,
					"layout": {},
					"paint": {
						"fill-color": "#ff9e67", //#ff9e67
						"fill-opacity": 1
					},
					"filter": ["==", "grid", ""]
				});
			}

			//-------------楼栋--------------------
			if (!map.getSource("building")) {
				map.addSource("building", this.getEmptySource());
			}

			if (!map.getLayer("build-fills")) {
                // 在point图层之下
                let underLayer = map.getLayer('point') ? "point" : "grid-borders";
                map.addLayer({
                    "id": "build-fills",
                    "type": "fill",
                    "source": "building",
                    "minzoom": 16,
                    "layout": {},
                    "paint": {
                        "fill-color": [
                            'case',
                            ['==', ['to-number', ['get', 'LEVEL']], 0], '#D4F2E7',
                            ['==', ['to-number', ['get', 'LEVEL']], 1], '#6ccdff',
                            ['==', ['to-number', ['get', 'LEVEL']], 2], '#DAA520',
                            ['==', ['to-number', ['get', 'LEVEL']], 3], '#FF8C00',
                            ['==', ['to-number', ['get', 'LEVEL']], 4], '#FF0000',
                            '#F0F8FF'
                        ],
                        "fill-opacity": 0.5
                    },
                    // "paint": {
                    // 	"fill-color": "#94CFEF",
                    // 	"fill-opacity": 0.5
                    // },
                }, underLayer);
			}

			if (!map.getLayer("build_hover1")) {
                // 在point图层之下
                let underLayer1 = map.getLayer('point') ? "point" : "";
                // 高亮颜色图层
                map.addLayer({
                    "id": "build_hover1",
                    "type": "line",
                    "source": "building",
                    "minzoom": 16,
                    "paint": {
                        "line-color": [
                            'case',
                            ['==', ['to-number', ['get', 'LEVEL']], 0], '#00BFFF',
                            ['==', ['to-number', ['get', 'LEVEL']], 1], '#4ca4fe',
                            ['==', ['to-number', ['get', 'LEVEL']], 2], '#BDB76B',
                            ['==', ['to-number', ['get', 'LEVEL']], 3], '#CD853F',
                            ['==', ['to-number', ['get', 'LEVEL']], 4], '#800000',
                            '#E6E6FA'
                        ],
                        "line-width": 0.5
                    },
                    // "paint": {
                    //     "line-color": "#2DB7F5",
                    //     "line-width": 1
                    // },
                }, underLayer1);
			}

			if (!map.getLayer("build_hover")) {
                // 在point图层之下
                let underLayer2 = map.getLayer('point') ? "point" : "";
				// 高亮颜色图层
				map.addLayer({
                    "id": "build_hover",
                    "type": "line",
                    "source": "building",
                    "minzoom": 16,
                    "paint": {
                        "line-color": [
                            'case',
                            ['==', ['to-number', ['get', 'LEVEL']], 0], '#00BFFF',
                            ['==', ['to-number', ['get', 'LEVEL']], 1], '#4ca4fe',
                            ['==', ['to-number', ['get', 'LEVEL']], 2], '#BDB76B',
                            ['==', ['to-number', ['get', 'LEVEL']], 3], '#CD853F',
                            ['==', ['to-number', ['get', 'LEVEL']], 4], '#800000',
                            '#E6E6FA'
                        ],
                        "line-width": 2
                    },
                    // "paint": {
                    //     "line-color": "#0863BE",
                    //     "line-width": 2
                    // },
                    // "filter": ["==", "JZWTYDZBM", ""]
                }, underLayer2);
			}

            if (!map.getLayer("build_choose_hover")) {
                // 在point图层之下
                let underLayer3 = map.getLayer('point') ? "point" : "";
				// 高亮颜色图层
				map.addLayer({
                    "id": "build_choose_hover",
                    "type": "line",
                    "source": "building",
                    "minzoom": 16,
                    "paint": {
                        "line-color": "#1E90FF",
                        "line-width": 2
                    },
                    "filter": ["==", "JZWTYDZBM", ""]
                }, underLayer3);
			}

			//------------边界----------------------------
			if (map.getSource("bianjie") == undefined) {
				map.addSource("bianjie", this.getEmptySource());
			}
			//边界的高亮图层
			if (map.getLayer("bianjie-highlighted_layer") == undefined) {
				// map.addLayer({
				// 	id: "bianjie-highlighted_layer",
				// 	type: "line",
				// 	source: "bianjie",
				// 	layout: {
				// 		"line-join": "round",
				// 		"line-cap": "round"
				// 	},
				// 	paint: {
				// 		"line-color": "#296CF2",
				// 		"line-width": 2
				// 	},
				// 	filter: ["==", "areacode", ""]
				// });


				map.addLayer({
					"id": "bianjie-highlighted-fills",
					"type": "fill",
					"source": "bianjie",
					"layout": {},
					"paint": {
						"fill-color": "#C0D7F8",
						"fill-opacity":0
					},
					// filter: ["==", "areacode", ""]
				});

				map.addLayer({
					"id": "bianjie-highlighted_layer",
					"type": "fill",
					"source": "bianjie",
					"layout": {},
					"paint": {
						"fill-color": "#C0D7F8",
						"fill-opacity": 0.4
					},
					filter: ["==", "areacode", ""]
				});
			}

			//------------选中区域----------------------------
			if (map.getSource("selectedArea") == undefined) {
				map.addSource("selectedArea", this.getEmptySource());
			}
			//边界的高亮图层
			if (map.getLayer("selectedArea-highlighted_layer") == undefined) {
				map.addLayer({
					id: "selectedArea-highlighted_layer",
					type: "line",
					source: "selectedArea",
					layout: {
						"line-join": "round",
						"line-cap": "round"
					},
					paint: {
						"line-color": "#296CF2",
						"line-width": 2
					},
				});

				map.addLayer({
					"id": "selectedArea-highlighted-fills",
					"type": "fill",
					"source": "selectedArea",
					"layout": {},
					"paint": {
						"fill-color": "#296CF2",
						"fill-opacity": 0.1

					},

				});



			}


			//------------小区 village----------------------------
			if (map.getSource("village") == undefined) {
				map.addSource("village", this.getEmptySource());
			}
			//边界的高亮图层
			if (map.getLayer("village-highlighted_layer") == undefined) {

				map.addLayer({
					"id": "village-highlighted-fills",
					"type": "fill",
					"source": "village",
					"minzoom": 15,
					"layout": {},
					"paint": {
						"fill-color": "#000000",
						"fill-opacity": 0.2

					},
					// filter: ["==", "areacode", ""]
				}, 'build-fills');
			}


			//------------道路 road----------------------------
			if (map.getSource("road") == undefined) {
				map.addSource("road", this.getEmptySource());
			}
			//边界的高亮图层
			if (map.getLayer("road-highlighted_layer") == undefined) {
				// map.addLayer({
				// 	id: "road-highlighted_layer",
				// 	type: "line",
				// 	source: "road",
				// 	"minzoom": 15,
				// 	layout: {
				// 		"line-join": "round",
				// 		"line-cap": "round"
				// 	},
				// 	paint: {
				// 		"line-color": "#ff0000",
				// 		"line-width": 2
				// 	},
				// 	filter: ["==", "areacode", ""]
				// });


				map.addLayer({
					"id": "road-highlighted-fills",
					"type": "fill",
					"source": "road",
					"minzoom": 15,
					"layout": {},
					"paint": {
						"fill-color": "#ff9e67",
						"fill-opacity": 0.7

					},
					// filter: ["==", "areacode", ""]
				});
			}


			resolve(true);
		});


	}

	/**
	 *设置Source数据源
	 * @param source  source名称
	 * @param data  features数组
	 */
	setSourceData(source, data) {
		let map = this._map;
		if (!!map.getSource(source)) {
			//设置数据
			if (!!data && data.length > 0) {
				map.getSource(source).setData({
					"type": "FeatureCollection",
					"features": data
				});
			} else if (Object.keys(data).length > 0 && !!data.type && data.type === 'FeatureCollection') {
				map.getSource(source).setData(data);
			} else {
				map.getSource(source).setData({
					"type": "FeatureCollection",
					"features": []
				});
			}

		} else {
			//生成一个同时设置数据
			map.addSource(source, this.getEmptySource(data));
		}

	}


	/**
	 * @description: 清空指定数据源
	 * @param source name
	 * @return:
	 */
	clearSourceData(source) {
		let map = this._map;
		if (!!map.getSource(source)) {
			//设置数据
			map.getSource(source).setData({
				"type": "FeatureCollection",
				"features": []
			});
		} else {
			//生成一个同时设置数据
			map.addSource(source, this.getEmptySource());
		}
	}


	/**
	 *
	 * 设置Layer颜色
	 * @param layerName  Layer名称
	 * @param color  颜色值
	 * @returns {boolean}  true操作成功；false Layer不存在
	 */
	setLayerColor(layerName, color) {
		let map = this._map;
		if (!!map.getLayer(layerName)) {
			if (map.getLayer(layerName).type === 'fill') {
				//面
				map.setPaintProperty(layerName, 'fill-color', color);
				return true;

			} else if (map.getLayer(layerName).type === 'line') {
				//线
				map.setPaintProperty(layerName, 'line-color', color);
				return true;
			}

		}

		return false;

	}

	/**
	 * 显示Layer
	 * @param layerName
	 * @returns {boolean} true操作成功；false Layer不存在
	 */
	setLayerShow(layerName) {
		let map = this._map;
		if (!!map.getLayer(layerName)) {
			let visibility = map.getLayoutProperty(layerName, 'visibility');
			if (visibility !== 'visible') {
				map.setLayoutProperty(layerName, 'visibility', 'visible');
			}
			return true;


		}
		return false;
	}

	/**
	 * 隐藏Layer
	 * @param layerName
	 * @returns {boolean} true操作成功；false Layer不存在
	 */
	setLayerHide(layerName) {
		let map = this._map;
		if (!!map.getLayer(layerName)) {
			let visibility = map.getLayoutProperty(layerName, 'visibility');
			if (visibility === 'visible') {
				map.setLayoutProperty(layerName, 'visibility', 'none');
			}
			return true;
		}
		return false;
	}


	// 获取界面可视范围
	getBounds() {
		let map = this._map;
		var _nw = map.unproject([0, 0]);
		var _ne = map.unproject([map.transform.width, 0]);
		var _sw = map.unproject([0, map.transform.height]);
		var _se = map.unproject([map.transform.width, map.transform.height]);
		return [
			[Math.min(_nw.lng, _ne.lng, _sw.lng, _se.lng), Math.min(_nw.lat, _ne.lat, _sw.lat, _se.lat)],
			[Math.max(_nw.lng, _ne.lng, _sw.lng, _se.lng), Math.max(_nw.lat, _ne.lat, _sw.lat, _se.lat)]
		];
	}


}

export {
	MapManager
}

export default MapManager
